from django.http import HttpResponse
from django.shortcuts import render
from .models import User  # 数据库模型类


# Create your views here.
def add_user(request):
    # 方法一
    # ajj = User(name="阿杰", age=18)
    # ajj.save()
    # 方法二
    # aj = User()
    # aj.name = "阿佳"
    # aj.age = 18
    # aj.save()
    # # 方法三
    # User.objects.create(name='墨染', age=30)
    # # 方法四
    # User.objects.get_or_create(name='指令', age=30)  # 有则获取，无则添加
    return HttpResponse("添加数据成功")


def select_user(request):
    # res = User.objects.all()  # 得到所有字段数据
    res = User.objects.get(id=1)
    print(res)
    return HttpResponse(res)


def update_user(request):
    # # 方法1
    # res = User.objects.get(id=1)
    # res.age = 20
    # res.save()
    # 方法二
    res = User.objects.filter(name='阿佳').update(age=19)
    return HttpResponse(res)


def delete_user(request):
    # User.objects.get(name='墨染').delete()  # 单行删除，如果有多个条件符合则会报错
    User.objects.filter(name='墨染').delete()  # 同一条件删除
    # User.objects.all().delete()  # 全部删除
    return HttpResponse('删除成功')
